package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.GlobalDetailMapper;
import com.kaili.basic.pojo.dto.MdmGlobalClobAddDto;
import com.kaili.basic.pojo.dto.MdmGlobalClobUpdateDto;
import com.kaili.basic.pojo.dto.MdmStrategyMainApplyAddDto;
import com.kaili.basic.pojo.dto.MdmStrategyMainSetAddDto;
import com.kaili.basic.pojo.entity.MdmGlobalDetailEntity;
import com.kaili.basic.pojo.entity.MdmStrategyMainEntity;
import com.kaili.basic.service.IGlobalDetailService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
*  service实现类
* @author:zhangjl
* @Time: 2019-08-07
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class GlobalDetailServiceImpl extends ServiceImpl<GlobalDetailMapper, MdmGlobalDetailEntity>  implements IGlobalDetailService {

    @Override
    public boolean add(Integer id,MdmStrategyMainSetAddDto dto) {

        List<MdmGlobalClobAddDto> list = dto.getGlobalClobList();
        for (MdmGlobalClobAddDto globalClobDto :list){
            MdmGlobalDetailEntity mdmGlobalDetailEntity = new MdmGlobalDetailEntity();
            BeanUtils.copyProperties(globalClobDto, mdmGlobalDetailEntity);
            mdmGlobalDetailEntity.setGlobalId(globalClobDto.getId());
            mdmGlobalDetailEntity.setStrategyId(id);
            mdmGlobalDetailEntity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
            mdmGlobalDetailEntity.setCreateTime(new Date());
            this.insert(mdmGlobalDetailEntity);
        }

        return true;
    }

    @Override
    public boolean addApply(Integer id,MdmStrategyMainApplyAddDto dto) {

        List<MdmGlobalClobAddDto> list = dto.getGlobalClobList();
        for (MdmGlobalClobAddDto globalClobDto :list){
            MdmGlobalDetailEntity mdmGlobalDetailEntity = new MdmGlobalDetailEntity();
            BeanUtils.copyProperties(globalClobDto, mdmGlobalDetailEntity);
            mdmGlobalDetailEntity.setGlobalId(globalClobDto.getId());
            mdmGlobalDetailEntity.setStrategyId(id);
            mdmGlobalDetailEntity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
            mdmGlobalDetailEntity.setCreateTime(new Date());
            this.insert(mdmGlobalDetailEntity);
        }

        return true;
    }

    @Override
    public boolean modify(MdmStrategyMainEntity entity, List<MdmGlobalClobUpdateDto> dtos) {
        //1、删除设备策略
       /* for (GlobalClobUpdateDto dto :dtos){
            this.del(entity.getId(), dto.getServiceCode());
        }*/
        //2、修改复选框数据
        for (MdmGlobalClobUpdateDto dto :dtos){
            MdmGlobalDetailEntity mdmGlobalDetailEntity = new MdmGlobalDetailEntity();
            mdmGlobalDetailEntity.setGlobalId(dto.getId());
            BeanUtils.copyProperties(dto, mdmGlobalDetailEntity);
            mdmGlobalDetailEntity.setGlobalId(mdmGlobalDetailEntity.getGlobalId());
            mdmGlobalDetailEntity.setStrategyId(entity.getId());
            mdmGlobalDetailEntity.setModifyTime(new Date());
            mdmGlobalDetailEntity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
            Wrapper<MdmGlobalDetailEntity> wrapper = new EntityWrapper<>();
            wrapper.where("global_id={0} and strategy_id ={1}", mdmGlobalDetailEntity.getGlobalId(), mdmGlobalDetailEntity.getStrategyId());
            this.update(mdmGlobalDetailEntity,wrapper);
        }

        return true;
    }

    @Override
    public boolean del(Integer id, String serviceCode) {
        Wrapper<MdmGlobalDetailEntity> wrapper = new EntityWrapper<>();
        wrapper.where("strategy_id={0} and service_code ={1}", id,serviceCode);
        this.delete(wrapper);
        return true;
    }
}
